package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.Users;
import com.kede.app.service.UsersService;
import com.kede.app.service.dto.UsersQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "users管理")
@RequestMapping("/api/usersphp")
public class UsersController {

    private final UsersService usersService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('usersphp:list')")
    public void exportUsers(HttpServletResponse response, UsersQueryCriteria criteria) throws IOException {
        usersService.download(usersService.queryAll(criteria), response);
    }

    /**
    * 查询users
    **/
    @GetMapping
    @Log("查询users")
    @ApiOperation("查询users")
    @PreAuthorize("@el.check('usersphp:list')")
    public ResponseEntity<Object> queryUsers(UsersQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(usersService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增users
    **/
    @PostMapping
    @Log("新增users")
    @ApiOperation("新增users")
    @PreAuthorize("@el.check('usersphp:add')")
    public ResponseEntity<Object> createUsers(@Validated @RequestBody Users resources){
        return new ResponseEntity<>(usersService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改users
    **/
    @PutMapping
    @Log("修改users")
    @ApiOperation("修改users")
    @PreAuthorize("@el.check('usersphp:edit')")
    public ResponseEntity<Object> updateUsers(@Validated @RequestBody Users resources){
        usersService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除users
    **/
    @DeleteMapping
    @Log("删除users")
    @ApiOperation("删除users")
    @PreAuthorize("@el.check('usersphp:del')")
    public ResponseEntity<Object> deleteUsers(@RequestBody Integer[] ids) {
        usersService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}